import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:hrf_app/common/index.dart';
import 'package:hrf_app/common/models/bug/project_bug_model.dart';
import 'package:hrf_app/common/services/user.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'index.dart';

class ProjectBugPage extends GetView<ProjectBugPageController> {
  const ProjectBugPage({super.key});

  Widget _buildView() {
    List<ProjectBugModel> projectBugList = controller.projectBugList;
    return Column(
      children: [
        _buildDownChunk(),
        Expanded(
            child: EasyRefresh(
          controller: controller.easyRefreshController,
          onRefresh: () async {
            print("下拉刷新逻辑");
            controller.onRefresh();
          },
          onLoad: () async {
            print("上拉加载逻辑");
            controller.onLoad();
          },
          child: ListView.builder(
              itemCount: projectBugList.length,
              itemBuilder: (context, index) {
                ProjectBugModel projectBugModel = projectBugList[index];
                return _buildItemView(projectBugModel);
              }),
        ))
      ],
    );
  }

  // 主视图
  Widget _buildItemView(ProjectBugModel projectBugModel) {
    return Container(
        margin: const EdgeInsets.all(8),
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: const [BoxShadow(color: Colors.grey, blurRadius: 0.3)],
          borderRadius: BorderRadius.circular(10),
        ),
        child: SizedBox(height: 130, child: _buildBugInfo(projectBugModel)));
  }

  Widget _buildBugInfo(ProjectBugModel projectBugModel) {
    int? priority = projectBugModel.priority;
    int? status = projectBugModel.status;
    String? assignUserId = projectBugModel.assignUserId;
    String priorityName = "低";
    TDTagTheme tagThemePriority = TDTagTheme.primary;
    if (priority == 2) {
      priorityName = "中";
      tagThemePriority = TDTagTheme.warning;
    }
    if (priority == 3) {
      priorityName = "高";
      tagThemePriority = TDTagTheme.danger;
    }
    String statusName = "未完成";
    TDTagTheme tagThemeStatus = TDTagTheme.danger;
    if (status == 5) {
      statusName = "已暂停";
      tagThemeStatus = TDTagTheme.warning;
    }
    if (status == 10) {
      statusName = "已解决";
      tagThemeStatus = TDTagTheme.primary;
    }
    if (status == 20) {
      statusName = "已关闭";
      tagThemeStatus = TDTagTheme.success;
    }
    String userId = UserService.to.profile.id!;
    Color assignNameColor = Colors.black;
    if (assignUserId == userId) {
      assignNameColor = Colors.red;
    }
    List<Widget> columList = [];
    columList.add(buildInfoDetailTitle("标题：", projectBugModel.title!));
    columList.add(buildInfoDetailIDLeftRight(
        Row(
          children: [
            itemTitle(
              "ID：",
            ),
            TDText(projectBugModel.idSort.toString()),
          ],
        ),
        Row(
          children: [
            itemTitle(
              "状态：",
            ),
            TDTag(statusName, theme: tagThemeStatus),
          ],
        )));
    columList.add(buildInfoDetailIDLeftRight(
      Row(
        children: [
          itemTitle(
            "项目：",
          ),
          TDText(projectBugModel.projectName!),
        ],
      ),
      Row(
        children: [
          itemTitle(
            "指派人：",
          ),
          Row(
            children: [
              TDText(
                projectBugModel.assignUserName == null
                    ? ""
                    : projectBugModel.assignUserName,
                textColor: assignNameColor,
              ),
              const SizedBox(
                width: 3,
              ),
              InkWell(
                  onTap: () {
                    controller.editAssignUserDialog(
                        projectBugModel.assignUserName ?? "",
                        "assignUserName",
                        "指派人",
                        projectBugModel.id!);
                  },
                  child: SvgPicture.asset(
                    AssetsSvgs.pointRight, // 使用 SVG 图标
                    width: 18,
                    height: 18,
                    color: Colors.blueAccent,
                  )).toVisibility(visible: projectBugModel.status == 0)
            ],
          )
        ],
      ),
    ));

    columList.add(buildInfoDetailIDLeftRight(
        Row(
          children: [
            itemTitle(
              "创建人：",
            ),
            TDText(projectBugModel.creatorName!),
          ],
        ),
        Row(
          children: [
            itemTitle(
              "优先级：",
            ),
            TDTag(
              priorityName,
              theme: tagThemePriority,
              isOutline: true,
            ),
          ],
        )));

    columList.add(
      buildInfoDetailTitle(
        "创建时间：",
        projectBugModel.createTime!,
      ),
    );
    return InkWell(
        onTap: () {
          controller.onJumpToDetail(projectBugModel.id);
        },
        child: Column(children: columList));
  }

//标题内容行
  Widget buildInfoDetailTitle(String title, String content) {
    return Row(
      children: [
        TDText(
          title,
          fontWeight: FontWeight.w700,
        ),
        Expanded(
          child: TDText(
            content,
            overflow: TextOverflow.ellipsis,
          ),
        )
      ],
    );
  }

//标题

  Widget buildInfoDetailIDLeftRight(Widget leftWidget, Widget rightWidget) {
    return Row(
      children: [
        Expanded(flex: 3, child: leftWidget),
        Expanded(flex: 3, child: rightWidget),
      ],
    );
  }

  TDDropdownMenu _buildDownChunk() {
    return TDDropdownMenu(
      direction: TDDropdownMenuDirection.down,
      items: [
        TDDropdownItem(
          label: '项目',
          options: controller.projectWidgetList,
          onChange: (value) {
            controller.projectOnChange(value);
          },
          onConfirm: (value) {},
          onReset: () {
            controller.projectOnReset();
          },
        ),
        TDDropdownItem(
          label: '人员',
          options: controller.userWidgetList,
          onChange: (value) {
            controller.userOnChange(value);
          },
          onReset: () {
            controller.userOnReset();
          },
        ),
      ],
    );
  }

  Widget itemTitle(title) {
    return TDText(
      title,
      fontWeight: FontWeight.w700,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ProjectBugPageController>(
      init: ProjectBugPageController(),
      id: "projectBugPage",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            title: const Text(
              "BUG管理",
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
          controller.   jumpToProjectButAdd();
            },
            tooltip: '增加',
            shape: CircleBorder(),
            child: Icon(Icons.add),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
